package edu.nf.waimai.dao.item.impl;

import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.item.ItemDAO;
import edu.nf.waimai.model.ltem.Item;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

public class ItemDAOImpl implements ItemDAO {
    /**
     * 添加商品
     * @param item
     * @return
     */
    @Override
    public int addItem(Item item) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "INSERT INTO items (shop_id, item_type_id, item_picture, item_name, item_price, item_state, item_details) VALUES (?,?,?,?,?,?,?)";
        return sqlExecutor.executeUpdate(sql,item.getShopId(),item.getItemTypeId(),item.getItemPicture(),item.getItemName(),item.getItemPrice(),item.getItemState(),item.getItemDetails());
    }

    /**
     * 删除商品
     * @param itemId
     * @return
     */
    @Override
    public int delItemById(int itemId) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "delete from items where item_id = ?";
        return sqlExecutor.executeUpdate(sql,itemId);
    }

    /**
     * 修改商品
     * @param item
     * @return
     */
    @Override
    public int updateItem(Item item) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "UPDATE items SET shop_id = ?,item_type_id = ?,item_picture = ?,item_name = ?,item_price = ?,item_state = ?,item_details=? WHERE item_id = ?";
        return sqlExecutor.executeUpdate(sql,item.getShopId(),item.getItemTypeId(),item.getItemPicture(),item.getItemName(),item.getItemPrice(),item.getItemState(),item.getItemDetails(),item.getItemId());
    }

    /**
     * 通过id修改商品的状态
     * @param itemId
     * @param state
     * @return
     */
    @Override
    public int updateItemStateById(int itemId, int state) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "update items set item_state=? where item_id=?";
        return sqlExecutor.executeUpdate(sql,state,itemId);
    }

    /**
     * 查询全部商品
     * @return
     */
    @Override
    public List<Item> listItem() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        BeanListHandler<Item> beanListHandler = new BeanListHandler<>(Item.class);
        String sql = "SELECT item_id, shop_id,item_type_id,item_picture,item_name,item_price,item_state,item_details FROM items";
        return sqlExecutor.executeQuery(sql,beanListHandler);
    }

    /**
     * 通过状态查询全部商品
     * @param state
     * @return
     */
    @Override
    public List<Item> listItemByState(int state) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        BeanListHandler<Item> beanListHandler = new BeanListHandler<>(Item.class);
        String sql = "SELECT item_id, shop_id,item_type_id,item_picture,item_name,item_price,item_state,item_details FROM items where item_state = ?";
        return sqlExecutor.executeQuery(sql,beanListHandler,state);
    }

    /**
     * 通过类型id和商家id查询全部商品
     * @param itemTypeId
     * @return
     */
    @Override
    public List<Item> listItemByItemTypeIdAndShopId(int itemTypeId,int shopId) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT item_id, shop_id,item_type_id,item_picture,item_name,item_price,item_state,item_details FROM items WHERE item_type_id = ? and shop_id = ?";
        BeanListHandler<Item> listHandler = new BeanListHandler<>(Item.class);
        return sqlExecutor.executeQuery(sql,listHandler,itemTypeId,shopId);
    }

    /**
     * 通过商家id查询全部商品
     * @param shopId
     * @return
     */
    @Override
    public List<Item> listItemsByShopId(int shopId) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT item_id, shop_id,item_type_id,item_picture,item_name,item_price,item_state,item_details FROM items WHERE shop_id = ?";
        BeanListHandler<Item> beanListHandler = new BeanListHandler<>(Item.class);
        return sqlExecutor.executeQuery(sql,beanListHandler,shopId);
    }

    @Override
    public List<Item> listItemByTypeId(int typeId) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        BeanListHandler<Item> beanListHandler = new BeanListHandler<>(Item.class);
        String sql = "SELECT item_id, shop_id,item_type_id,item_picture,item_name,item_price,item_state,item_details FROM items where item_type_id = ?";
        return sqlExecutor.executeQuery(sql,beanListHandler,typeId);
    }
}
